import { Component } from '@angular/core';
import { NavController, NavParams, ViewController } from 'ionic-angular';
import { HttpDateProvider } from '../../providers/http-date/http-date';
import { Task } from '../home/task';

/**
 * Generated class for the InputTaskPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-input-task',
  templateUrl: 'input-task.html',
})
export class InputTaskPage {

  title:string;
  description:string;
  status:string='open';
  objectId:string;

  action:string="新增";

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    public viewCtrl:ViewController,
    public http:HttpDateProvider) {
  }

  ionViewDidLoad() {
    let task:Task=this.navParams.get("task");
    if(task){
      this.title=task.title;
      this.status=task.status;
      this.description=task.description;
      this.objectId=task.objectId;
      this.action='编辑';
    }
  }

  close(){
    this.viewCtrl.dismiss();
  }

  saveTask(){
    let _task:Task={
      title:this.title,
      status:this.status,
      description:this.description,
      objectId:this.objectId
    };
    if(this.action==='新增'){
      this.http.addTask(_task).subscribe(data=>_task.objectId=data.objectId);
    }else if(this.action==='编辑'){
      this.http.updateTask(_task).subscribe();
    }
    this.viewCtrl.dismiss(_task);
  }

}
